package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsBrandService;
import com.qf.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class DtsBrandServiceImpl implements DtsBrandService {

    @Autowired
    DtsBrandMapper dtsBrandMapper;

    @Override
    public List<Integer> findByAdminById(Integer id) {
        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getAdminId,id);
        qw.select(DtsBrand::getId);
        List<DtsBrand> brands = dtsBrandMapper.selectList(qw);
        List<Integer> brandIds = brands.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());

        return brandIds;
    }

    @Override
    public List<Map<String,Object>> findBrand() {
        List<DtsBrand> brands = dtsBrandMapper.selectList(null);
        List<Map<String, Object>> data = brands.stream().map(dtsBrand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsBrand.getId());
            map.put("label", dtsBrand.getName());
            return map;
        }).collect(Collectors.toList());
        return data;
    }

    @Autowired
    StatMapper statMapper;

    @Override
    public IPage<DtsBrandVo> findByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        QueryWrapper<DtsBrand> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        if(id!=null){
            qw.like("id",id);
        }

        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        dtsBrandMapper.selectPage(iPage,qw);
        IPage<DtsBrandVo> iPage1 = new Page<>();
        List<DtsBrandVo> dtsBrandVos = new ArrayList<>();
        List<DtsBrand> records = iPage.getRecords();
        for (DtsBrand record : records) {
            DtsBrandVo dtsBrandVo = new DtsBrandVo();
            dtsBrandVo.setId(record.getId());
            dtsBrandVo.setName(record.getName());
            dtsBrandVo.setDesc(record.getDesc());
            dtsBrandVo.setPicUrl(record.getPicUrl());
            dtsBrandVo.setSortOrder(record.getSortOrder());
            dtsBrandVo.setFloorPrice(record.getFloorPrice());
            dtsBrandVo.setAddTime(record.getAddTime());
            dtsBrandVo.setUpdateTime(record.getUpdateTime());
            dtsBrandVo.setShareUrl(record.getShareUrl());
            dtsBrandVo.setAdminId(record.getAdminId());
            dtsBrandVo.setDeleted(record.getDeleted());
            dtsBrandVo.setCommpany(record.getCommpany());
            dtsBrandVo.setAutoUpdateGood(record.getAutoUpdateGood());
            dtsBrandVo.setDefaultCategoryId(record.getDefaultCategoryId());
            dtsBrandVo.setDefaultPages(record.getDefaultPages());
            dtsBrandVo.setAddPrecent(record.getAddPrecent());

            Integer defaultCategoryId = record.getDefaultCategoryId();
            Map<String,Object> map = statMapper.findPidAndId(defaultCategoryId);
            Integer pid = (Integer) map.get("pid");
            Integer id1 = (Integer) map.get("id");
            Integer[] integers = new Integer[] {pid,id1};
            dtsBrandVo.setCategoryIds(integers);
            dtsBrandVos.add(dtsBrandVo);


        }
        iPage1.setRecords(dtsBrandVos);
        iPage1.setTotal(iPage.getTotal());
        return iPage1;
    }

    @Override
    public DtsBrand add(DtsBrand brand) {
        brand.setAddTime(new Date());
        brand.setDeleted(false);
        dtsBrandMapper.insert(brand);
        return brand;
    }

    @Override
    public DtsBrand update(DtsBrand dtsBrand) {
        dtsBrand.setDeleted(false);
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.updateById(dtsBrand);
        return dtsBrand;
    }

    @Override
    public void delete(DtsBrand dtsBrand) {
        dtsBrandMapper.deleteById(dtsBrand);
    }
}
